<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
    <style>
        .active {
            background-color: green;
        }

        .text-danger {
            color: red;
        }
    </style>
</head>

<body>
    <div id="app">
        <!-- class -->
        <!-- 对象语法 -->
        <div v-bind:class="{active:isActive}">{active:isActive}</div>

        <!-- 你可以在对象中传入更多字段来动态切换多个 class。此外，v-bind:class 指令也可以与普通的 class attribute 共存。当有如下模板： -->
        <div class="static" v-bind:class="{ active: isActive, 'text-danger': hasError }">{ active: isActive,
            'text-danger': hasError }</div>

        <!-- 或者  绑定的数据对象不必内联定义在模板里： -->
        <div class="static" v-bind:class="classObject">classObject</div>
    </div>
    <script>
        new Vue({
            el: "#app",
            data: {
                isActive: false,
                hasError: true,
                // classObject: {
                //     active: true,
                //     'text-danger': false
                // }
            },
            computed: {
                classObject: function () {
                    return {
                        active: this.isActive ,
                        'text-danger': this.error 
                    }
                }
            }
        }) 
    </script>
</body>

</html>